home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 August / August CD.bin / Shareware / Education / numericalmethods Folder / chap_3 / a3_4.m < prev    next >
Encoding:
Text File  |  1994-06-05  |  2.5 KB  |  105 lines  |  [MATS/MATL]

  1. echo off;
  2. % NUMERICAL METHODS: MATLAB Programs, (c) John H. Mathews 1994
  3. % To accompany the text:
  4. % NUMERICAL METHODS for Mathematics, Science and Engineering, 2nd Ed, 1992
  5. % Prentice Hall, Englewood Cliffs, New Jersey, 07632, U.S.A.
  6. % This free software is complements of the author.
  7.  
  8. % Algorithm 3.4 (Jacobi Iteration).
  9. % Section    3.7,    Iterative Methods for Linear Systems, Page 186
  10. echo on; clc; format long; clear
  11.  
  12. % This program solves a linear system AX = B.
  13.  
  14. % Where A is diagonally dominant.
  15.  
  16. % A is an n x n matrix, B is an n-dimensional vector.
  17.  
  18. % The method is Jacobi fixed point iteration.
  19.  
  20. % Remark. jacobi.m is used for Algorithm 3.4
  21.  
  22. pause % Press any key to perform Jacobi iteration.
  23.  
  24. clc;
  25. % Solve the system AX = B:
  26.  
  27. A = [ 4   -1    1;
  28.       4   -8    1;
  29.      -2    1    5];
  30.  
  31. B = [ 7; -21;  15];         % Enter B as a column vector.
  32.  
  33. pause % Press any key to continue.
  34.  
  35. clc;
  36. %    Place the starting vector in  P
  37.  
  38. % Place the tolerance in  delta
  39.  
  40. %    Place the number of iterations in  max1
  41.  
  42. P = [ 1;   2;   2];         % Enter P as a column vector.
  43. delta = 1e-12;
  44. max1 = 50;
  45.  
  46. [X,dX,Pm] = jacobi(A,B,P,delta,max1);
  47.  
  48. pause % Press any key to continue.
  49.  
  50. Mx1 = 'Computations for Jacobi iteration.';
  51. Mx2 = '     x                  y                  z';
  52. Mx3 = 'The matrix is A =';
  53. Mx4 = 'The vector B is displayed as B` =';
  54. Mx5 = 'The solution X is displayed as X`  = ';
  55. Mx6 = 'The accuracy is  +- dX,  where dX` = ';
  56. clc,echo off,diary output,...
  57. disp(''),disp(Mx1),disp(Mx2),disp(Pm),...
  58. diary off,echo on
  59. pause % Press any key to continue.
  60. echo off,diary output,...
  61. disp(Mx3),disp(A),disp(Mx4),disp(B'),...
  62. disp(Mx5),disp(X'),disp(Mx6),disp(dX'),...
  63. disp('Iteration is converging linearly to the solution.'),...
  64. diary off,echo on
  65.  
  66. pause % Press any key to perform Jacobi iteration.
  67.  
  68. clc;
  69. % Solve the system:
  70.  
  71. A = [-2    1    5;
  72.       4   -8    1;
  73.       4   -1    1];
  74.  
  75. B = [15; -21;   7];        % Enter P as a column vector.
  76.  
  77. pause % Press any key to continue.
  78.  
  79. clc;
  80. %    Place the starting vector in  P
  81.  
  82. % Place the tolerance in  delta
  83.  
  84. %    Place the number of iterations in  max1
  85.  
  86.  
  87. P = [ 1;   2;   2];         % Enter P as a column vector.
  88. delta = 1e-12;
  89. max1 = 4;
  90.  
  91. [X,dX,Pm] = jacobi(A,B,P,delta,max1);
  92.  
  93. pause % Press any key to continue.
  94.  
  95. clc,echo off,diary output,...
  96. disp(''),disp(Mx1),disp(Mx2),disp(Pm),...
  97. diary off,echo on
  98. pause % Press any key to continue.
  99. echo off,diary output,...
  100. disp(Mx3),disp(A),disp(Mx4),disp(B'),...
  101. disp(Mx5),disp(X'),disp(Mx6),disp(dX'),...
  102. disp('Iteration is diverging to infinity.'),...
  103. disp('Notice the "scale factor" for the output.'),...
  104. diary off,echo on
  105.